package business.manage.user.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import common.util.PojoChangeToMap;

import business.manage.user.UserService;
import business.manage.user.argument.UserArgument;
import business.manage.user.dto.User;

@Controller
@RequestMapping("admin")
public class UserController {
	Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private UserService userService;
	
	@RequestMapping("/toLogin")
	public String toLogin(){
		return "admin/login";
	}
	
	@RequestMapping("/login")
	public String login(HttpSession httpSession,@ModelAttribute UserArgument userArgument){
		logger.info("用户登陆:"+PojoChangeToMap.change(userArgument));
		try {
			User user = userService.findUser(userArgument);
			if(user!=null){
				httpSession.setAttribute("user", user);
				return "admin/default";
			}else{
				return toLogin();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return toLogin();
		}
	}
	
	@RequestMapping("/default")
	public String default_(){
		return "admin/main";
	}
	
	
	@RequestMapping("logout")
	public String logout(HttpSession httpSession){
		Object object = httpSession.getAttribute("user");
		if(object!=null){
			httpSession.removeAttribute("user");
		}
		return "admin/login";
	}
}
